package kernel;

import java.util.ArrayList;

import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class GoogleTalkConnection {
	private ArrayList<ConnectionServerListener> _connectionServerListeners;
	private XMPPConnection _connection;
	
	public GoogleTalkConnection()
	{
		_connectionServerListeners = new ArrayList<ConnectionServerListener>();
		_connection = new XMPPConnection(new ConnectionConfiguration("talk.google.com",5222,"gmail.com"));
	}
	
	public void connect() throws XMPPException{
		_connection.connect();
		if(_connection.isConnected())
			FireConnectedToServer();
	}
	
	public void login(String username, String password) throws XMPPException{
		_connection.login(username, password);
	}

	public ChatManager getChatManager() {
		return _connection.getChatManager();
	}

	public String getUser() {
		return _connection.getUser();
	}

	public Roster getRoster() {
		return _connection.getRoster();
	}

	public boolean isConnected() {
		return _connection.isConnected();
	}

	public void disconnect() {
		_connection.disconnect();
		if(!_connection.isConnected())
			FireDisconnectedToServer();
	}

	public void addConnectionListener(ConnectionListener connectionListener) {
		_connection.addConnectionListener(connectionListener);
	}

	public void removeConnectionListener(ConnectionListener connectionListener) {
		_connection.removeConnectionListener(connectionListener);
	}
	
	private void FireConnectedToServer()
	{
		for (ConnectionServerListener listener : _connectionServerListeners) {
			listener.ConnectedToServer();
		}
	}
	private void FireDisconnectedToServer()
	{
		for (ConnectionServerListener listener : _connectionServerListeners) {
			listener.DisconnectedFromServer();
		}
	}

	public AccountManager getAccountManager() {
		return _connection.getAccountManager();
	}
}
